from peewee import BigAutoField, CharField, BigIntegerField, IntegerField, BooleanField
from common.model.abstrct import AbstractModel, IntEnumField, AbstractModelWithID
from enums.pay_enum import PayWayStatusEnum
from enums.tenant_enum import TenantStatusEnum, TenantSidebarPermissionEnum


class Tenant(AbstractModel):

    class Meta:
        table_name = "tb_tenant"
        auto_increment_base = 1_000_000
        indexes = (
            (("slug", ), True),
            (("code", ), True),
        )

    tenant_id = BigAutoField(primary_key=True, help_text="租户id")
    name = CharField(max_length=64, help_text="租户的名字")
    code = CharField(max_length=16, help_text="租户唯一code,自动生成", null=True)
    invited_code = CharField(max_length=10, help_text="商户受邀码", null=True)
    status = IntEnumField(default=TenantStatusEnum.wait_verify, help_text="租户状态", choices=TenantStatusEnum)
    # 如果slug和domain都有的话,表示既可以通过租户自己的域名进主页,也可以通过我们的二级域名进主页
    slug = CharField(max_length=16, help_text="二级域名,如果有这个表示使用我们的域名而不是租户自己的域名")
    domain = CharField(max_length=64, null=True, help_text="域名,和slug二选一 http/https开头")
    # 主页的icon
    icon = CharField(max_length=255, null=True, help_text="租户的icon")
    # 主页的title
    title = CharField(max_length=32, null=True, help_text="租户title")
    owner_id = BigIntegerField(help_text="当前租户所属用户id")


class TenantConfig(AbstractModelWithID):

    class Meta:
        table_name = "tb_tenant_config"
        indexes = (
            (("tenant_id", ), True),
        )
    tenant_id = BigIntegerField(help_text="租户id")
    m_secret = CharField(max_length=32, help_text="管理端用户信息加密密钥")
    c_secret = CharField(max_length=32, help_text="C端用户信息加密密钥")

    # 租户自己的服务访问的凭证
    appid = CharField(max_length=16, help_text="唯一id")
    app_secret = CharField(max_length=32, help_text="密钥")


class MiddleTenantPayWay(AbstractModelWithID):
    class Meta:
        table_name = "tb_mid_tenant_pay_way"
        indexes = (
            (("tenant_id", "pay_way_id"), True),
        )

    tenant_id = BigIntegerField(help_text="租户id")
    pay_way_id = IntegerField(help_text="支付方式id")
    status = IntEnumField(default=PayWayStatusEnum.enable, help_text="表示当前商户是否对当前支付方式可用",
                          choices=PayWayStatusEnum)


# 租户可用的侧边栏权限
class MiddleTenantSidebarPermission(AbstractModelWithID):

    class Meta:
        table_name = "tb_mid_tenant_sidebar_permission"
        indexes = (
            (("tenant_id",), False),
            (("sidebar_permission_id",), False),
        )

    tenant_id = BigIntegerField(help_text="租户id")
    sidebar_permission_id = IntegerField(help_text="侧边栏权限id")
    # 加limit的原因就是 侧边栏受限使用
    status = IntEnumField(default=TenantSidebarPermissionEnum.enable, help_text="租户侧边栏状态",
                          choices=TenantSidebarPermissionEnum)


class MiddleTenantPermission(AbstractModelWithID):

    class Meta:
        table_name = "tb_mid_tenant_permission"
        indexes = (
            (("tenant_id", ), False),
            (("permission_id", ), False),
        )

    tenant_id = BigIntegerField(help_text="租户id")
    permission_id = IntegerField(help_text="权限id")


# tenant 和 mall 之间的关系
class MiddleTenantMall(AbstractModelWithID):

    class Meta:
        table_name = "tb_mid_tenant_mall"
        indexes = (
            (("tenant_id", "mall_code"), False),
            (("tenant_id", "mall_id"), False),
        )

    tenant_id = BigIntegerField(help_text="tenant id")
    mall_code = CharField(help_text="mall code")
    mall_id = BigIntegerField(help_text="mall id")


# tenant和币种的关系
class MiddleTenantCurrency(AbstractModelWithID):

    class Meta:
        table_name = "tb_mid_tenant_currency"
        indexes = (
            (("tenant_id", "currency_code"), True),
        )
    tenant_id = BigIntegerField(help_text="tenant id")
    currency_code = CharField(max_length=3, help_text="币种代码")
    is_default = BooleanField(default=False, help_text="是否是默认币种,每个tenant只能有一个默认币种")


# tenant和语言的关系
class MiddleTenantLanguage(AbstractModelWithID):

    class Meta:
        table_name = "tb_mid_tenant_language"
        indexes = (
            (("tenant_id", "language_code"), True),
        )
    tenant_id = BigIntegerField(help_text="tenant id")
    language_code = CharField(max_length=8, help_text="语言code")
    is_default = BooleanField(default=False, help_text="是否是默认语言,每个tenant只能有一种默认语言")
